package com.qyhl.module_activities.act.live;

import android.app.Activity;

import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.act.TeleVoteBean;
import com.qyhl.webtv.commonlib.service.UserService;

import java.text.SimpleDateFormat;

public class TeleVotePresenter implements TeleVoteContract.TeleVotePresenter {
    public static final int STATUS_NOLOGIN = 1;
    public static final int STATUS_BEGIN = 2;
    public static final int STATUS_BEFORE = 3;
    public static final int STATUS_AFTER = 4;
    public static int STATUS = -1;

    private TeleVoteContract.TeleVoteView view;
    private TeleVoteContract.TeleVoteModel model;

    public TeleVotePresenter(TeleVoteContract.TeleVoteView view) {
        this.view = view;
        model = new TeleVoteModel(this);
    }

    @Override
    public void getData(final Activity activity, final String actId, final String linkId) {
        STATUS = -1;

        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
            @Override
            public void login(boolean isLogin) {
                if (isLogin) {
                    model.getData(actId, linkId, CommonUtils.getInstance().getUserName());
                } else {
                    STATUS = STATUS_NOLOGIN;
                    model.getData(actId, linkId, "0");
                }
            }

            @Override
            public void error(String s) {
                STATUS = STATUS_NOLOGIN;
                model.getData(actId, linkId, "0");
            }
        });

    }

    @Override
    public void setError(String msg) {
        if (view != null) {
            view.setError(msg);
        }
    }

    @Override
    public void setDetail(TeleVoteBean data) {
        if (view != null) {
            if (data.getEntityList() == null || data.getEntityList().isEmpty()) {
                view.setError("暂无内容");
            } else {
                if (STATUS != STATUS_NOLOGIN) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        long star = sdf.parse(data.getSurveyAct().getStartTime()).getTime();
                        long end = sdf.parse(data.getSurveyAct().getEndTime()).getTime();
                        long now = System.currentTimeMillis();
                        if (now < star) {
                            STATUS = STATUS_BEFORE;
                        } else if (now > end) {
                            STATUS = STATUS_AFTER;
                        } else {
                            STATUS = STATUS_BEGIN;
                        }
                    } catch (Exception e) {
                        STATUS = STATUS_BEFORE;
                        e.printStackTrace();
                    }
                }
                view.setDetail(data);
            }
        }
    }

    @Override
    public void onDestroy() {
        view = null;
    }
}
